package javacommon.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * create time :Jun 4, 2009
 * @author : nail
 *
 */
public class FileUtils {

	public FileUtils() {
	}

	/**
	 * 
	 * create time :Jun 4, 2009
	 * @author : nail
	 *
	 * @param sfile
	 * @param dfile
	 * @return
	 */
	public int copyFile(String sfile, String dfile) {
		int n;
		byte[] buffer = new byte[1024];
		if (sfile == null || sfile.equals(""))
			return -1;
		if (dfile == null || dfile.equals(""))
			return -1;
		try {
			File file = new File(sfile);

			if (file.isFile()) {
				File file2 = new File(dfile);
				//�ж�Ŀ���ļ��ǲ����ļ���������ļ��ͼ���
				FileInputStream fis = new FileInputStream(file);
				FileOutputStream fos = new FileOutputStream(file2);
				while ((n = fis.read(buffer)) != -1) {
					fos.write(buffer, 0, n);
				}
				fos.close();
				fis.close();

			}
			if (file.isDirectory()) {
				File file2 = new File(dfile);
				//����Ŀ��Ŀ¼
				file2.mkdir();
				String[] list = file.list();
				//�ж��ļ������Ƿ����ļ�
				if (list != null && list.length > 0) {
					for (int i = 0; i < list.length; i++) {
						String temppath = file.getPath().replace('\\', '/');
						//temppath2��ŵ�ǰĿ��·��
						String temppath2 = file2.getPath().replace('\\', '/');
						copyFile(temppath + "/" + list[i], temppath2 + "/"
								+ list[i]);

					}
				}

			}
		} catch (Exception e) {
			return -1;
		}
		return 0;
	}

	public static void ReadData() {
		try {
			FileReader read = new FileReader("D:/country.txt");
			BufferedReader br = new BufferedReader(read);
			String row;
			while ((row = br.readLine()) != null) {
				System.out.println(row +" === ");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public  static List getDataFromTxt() {
		List list = new ArrayList();
		try {
			
			FileReader read = new FileReader("D:\\my-project\\projects\\aidingba\\database\\city.txt");
			BufferedReader br = new BufferedReader(read);
			String row;
			while ((row = br.readLine()) != null) {
				Map map = new HashMap();
				String[] rows = row.split(",");
				if(rows.length>1)
				{
					map.put("code", rows[0].trim());
					map.put("name", rows[1].trim());
					map.put("id", rows[2].trim());
					map.put("pid", rows[3].trim());
				//System.out.println(rows[0].trim() +"==="+rows[1].trim());
				}
				else
				{
					//System.out.println("************* " +row);
				}
				
				list.add(map);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static void main(String[] args) {
		getDataFromTxt();

	}

}